<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.0">
  <link rel="apple-touch-icon" sizes="180x180" href="/myblog/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/myblog/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/myblog/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/myblog/images/logo.svg" color="#222">

<link rel="stylesheet" href="/myblog/css/main.css">


<link rel="stylesheet" href="/myblog/lib/font-awesome/css/all.min.css">
  <link rel="stylesheet" href="/myblog/lib/pace/pace-theme-minimal.min.css">
  <script src="/myblog/lib/pace/pace.min.js"></script>

<script id="hexo-configurations">
    var NexT = window.NexT || {};
    var CONFIG = {"hostname":"gao-qianwei.gitee.io","root":"/myblog/","scheme":"Gemini","version":"7.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12,"onmobile":false},"copycode":{"enable":false,"show_result":false,"style":null},"back2top":{"enable":true,"sidebar":false,"scrollpercent":false},"bookmark":{"enable":false,"color":"#222","save":"auto"},"fancybox":false,"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"algolia":{"hits":{"per_page":10},"labels":{"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}},"localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false},"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},"path":"search.xml"};
  </script>

  <meta name="description" content="Map 方法之后，Reduce 方法之前的数据处理过程称之为 Shuffle。">
<meta property="og:type" content="article">
<meta property="og:title" content="shuffle运行机制">
<meta property="og:url" content="https://gao-qianwei.gitee.io/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/index.html">
<meta property="og:site_name" content="学无止境">
<meta property="og:description" content="Map 方法之后，Reduce 方法之前的数据处理过程称之为 Shuffle。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/1.png">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/2.png">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/3.png">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/4.png">
<meta property="og:image" content="https://gao-qianwei.gitee.io/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/5.png">
<meta property="article:published_time" content="2021-06-06T10:17:17.705Z">
<meta property="article:modified_time" content="2021-06-06T10:32:08.832Z">
<meta property="article:author" content="高乾威">
<meta property="article:tag" content="数据研发">
<meta property="article:tag" content="Hadoop">
<meta property="article:tag" content="MapReduce">
<meta property="article:tag" content="shuffle">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gao-qianwei.gitee.io/myblog/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/1.png">

<link rel="canonical" href="https://gao-qianwei.gitee.io/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/">


<script id="page-configurations">
  // https://hexo.io/docs/variables.html
  CONFIG.page = {
    sidebar: "",
    isHome : false,
    isPost : true,
    lang   : 'zh-CN'
  };
</script>

  <title>shuffle运行机制 | 学无止境</title>
  






  <noscript>
  <style>
  .use-motion .brand,
  .use-motion .menu-item,
  .sidebar-inner,
  .use-motion .post-block,
  .use-motion .pagination,
  .use-motion .comments,
  .use-motion .post-header,
  .use-motion .post-body,
  .use-motion .collection-header { opacity: initial; }

  .use-motion .site-title,
  .use-motion .site-subtitle {
    opacity: initial;
    top: initial;
  }

  .use-motion .logo-line-before i { left: initial; }
  .use-motion .logo-line-after i { right: initial; }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage">
  <div class="container use-motion">
    <div class="headband"></div>

    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏">
      <span class="toggle-line toggle-line-first"></span>
      <span class="toggle-line toggle-line-middle"></span>
      <span class="toggle-line toggle-line-last"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/myblog/" class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <h1 class="site-title">学无止境</h1>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>




<nav class="site-nav">
  <ul id="menu" class="main-menu menu">
        <li class="menu-item menu-item-home">

    <a href="/myblog/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a>

  </li>
        <li class="menu-item menu-item-about">

    <a href="/myblog/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a>

  </li>
        <li class="menu-item menu-item-tags">

    <a href="/myblog/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a>

  </li>
        <li class="menu-item menu-item-categories">

    <a href="/myblog/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a>

  </li>
        <li class="menu-item menu-item-archives">

    <a href="/myblog/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a>

  </li>
        <li class="menu-item menu-item-links">

    <a href="/myblog/links/" rel="section"><i class="fa fa-link fa-fw"></i>友链</a>

  </li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup">
        <div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div id="search-result">
  <div id="no-result">
    <i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>
  </div>
</div>

    </div>
  </div>

</div>
    </header>

    
  <div class="back-to-top">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>


    <main class="main">
      <div class="main-inner">
        <div class="content-wrap">
          

          <div class="content post posts-expand">
            

    
  
  
  <article itemscope itemtype="http://schema.org/Article" class="post-block" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://gao-qianwei.gitee.io/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/myblog/images/avatar.png">
      <meta itemprop="name" content="高乾威">
      <meta itemprop="description" content="报我楼成秋望月，把君诗读夜回灯">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="学无止境">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          shuffle运行机制
        </h1>

        <div class="post-meta">

          
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-calendar"></i>
              </span>
              <span class="post-meta-item-text">发表于</span>
              

              <time title="创建时间：2021-06-06 18:17:17 / 修改时间：18:32:08" itemprop="dateCreated datePublished" datetime="2021-06-06T18:17:17+08:00">2021-06-06</time>
            </span>
            <span class="post-meta-item">
              <span class="post-meta-item-icon">
                <i class="far fa-folder"></i>
              </span>
              <span class="post-meta-item-text">分类于</span>
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/%E6%95%B0%E6%8D%AE%E7%A0%94%E5%8F%91/" itemprop="url" rel="index"><span itemprop="name">数据研发</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/%E6%95%B0%E6%8D%AE%E7%A0%94%E5%8F%91/Hadoop/" itemprop="url" rel="index"><span itemprop="name">Hadoop</span></a>
                </span>
                  ，
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/myblog/categories/%E6%95%B0%E6%8D%AE%E7%A0%94%E5%8F%91/Hadoop/MapReduce/" itemprop="url" rel="index"><span itemprop="name">MapReduce</span></a>
                </span>
            </span>

          
            <span class="post-meta-item" title="阅读次数" id="busuanzi_container_page_pv" style="display: none;">
              <span class="post-meta-item-icon">
                <i class="fa fa-eye"></i>
              </span>
              <span class="post-meta-item-text">阅读次数：</span>
              <span id="busuanzi_value_page_pv"></span>
            </span>
            <span class="post-meta-divider">|</span>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">

      
        <p>    Map 方法之后，Reduce 方法之前的数据处理过程称之为 Shuffle。</p>
<span id="more"></span>

<h3 id="一、MapReduce详细工作流程"><a href="#一、MapReduce详细工作流程" class="headerlink" title="一、MapReduce详细工作流程"></a>一、MapReduce详细工作流程</h3><img src="/myblog/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/1.png" alt="image-20210531210018349" style="zoom:80%;">

<img src="/myblog/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/2.png" alt="image-20210531210054911" style="zoom: 80%;">

<p>&ensp;&ensp;&ensp;&ensp;上面的流程是整个 MapReduce 最全工作流程，但是 Shuffle 过程只是从第 7 步开始到第 16 步结束，具体 Shuffle 过程详解，如下： </p>
<p>（1）MapTask 收集我们的 map() 方法输出的 kv 对，放到内存缓冲区中 </p>
<p>（2）从内存缓冲区不断溢出本地磁盘文件，可能会溢出多个文件 </p>
<p>（3）多个溢出文件会被合并成大的溢出文件 </p>
<p>（4）在溢出过程及合并的过程中，都要调用 Partitioner 进行分区和针对 key 进行排序 </p>
<p>（5）ReduceTask 根据自己的分区号，去各个 MapTask 机器上取相应的结果分区数据 </p>
<p>（6）ReduceTask 会抓取到同一个分区的来自不同 MapTask 的结果文件，ReduceTask 会将这些文件再进行合并（归并排序） </p>
<p>（7）合并成大文件后，Shuffle 的过程也就结束了，后面进入 ReduceTask 的逻辑运算过 程（从文件中取出一个一个的键值对 Group，调用用户自定义的 reduce()方法） </p>
<p>&ensp;&ensp;&ensp;&ensp;注意：</p>
<p> &ensp;&ensp;&ensp;&ensp;（1）Shuffle 中的缓冲区大小会影响到 MapReduce 程序的执行效率，原则上说，缓冲区 越大，磁盘 io 的次数越少，执行速度就越快。 </p>
<p>&ensp;&ensp;&ensp;&ensp;（2）缓冲区的大小可以通过参数调整，参数：mapreduce.task.io.sort.mb 默认 100M。</p>
<h3 id="二、shuffle机制"><a href="#二、shuffle机制" class="headerlink" title="二、shuffle机制"></a>二、shuffle机制</h3><h4 id="2-1-概述"><a href="#2-1-概述" class="headerlink" title="2.1 概述"></a>2.1 概述</h4><ol>
<li> Map 方法之后，Reduce 方法之前的数据处理过程称之为 Shuffle；</li>
<li> mapreduce中，map阶段处理的数据如何传递给reduce阶段，是mapreduce框架中最关键的一个流程，这个流程就叫shuffle；</li>
<li> shuffle: 洗牌、发牌——（核心机制：数据分区，排序，缓存）；</li>
<li> 具体来说：就是将maptask输出的处理结果数据，分发给reducetask，并在分发的过程中，对数据按key进行了分区和排序；</li>
</ol>
<h4 id="2-2-主要流程"><a href="#2-2-主要流程" class="headerlink" title="2.2 主要流程"></a>2.2 主要流程</h4><p><img src="/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/3.png" alt="img"></p>
<p>&ensp;&ensp;&ensp;&ensp;shuffle是MR处理流程中的一个过程，它的每一个处理步骤是分散在各个map task和reduce task节点上完成的，整体来看，分为3个操作：</p>
<ol>
<li> 分区partition</li>
<li> sort根据key排序(快排)</li>
<li> Combiner进行局部value的合并</li>
</ol>
<h4 id="2-3-详细流程"><a href="#2-3-详细流程" class="headerlink" title="2.3 详细流程"></a>2.3 详细流程</h4><img src="/myblog/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/4.png" alt="image-20210601000918949" style="zoom:67%;">

<ol>
<li> maptask收集我们的map()方法输出的kv对，放到内存缓冲区中</li>
<li> 从内存缓冲区不断溢出本地磁盘文件，可能会溢出多个文件 （默认100M）</li>
<li> 多个溢出文件会被合并成大的溢出文件</li>
<li> 在溢出过程中，及合并的过程中，都要调用partitoner进行分组和针对key进行排序</li>
<li> reducetask根据自己的分区号，去各个maptask机器上取相应的结果分区数据</li>
<li> reducetask会取到同一个分区的来自不同maptask的结果文件，reducetask会将这些文件再进行合并（归并排序）</li>
<li> 合并成大文件后，shuffle的过程也就结束了，后面进入reducetask的逻辑运算过程（从文件中取出一个一个的键值对group，调用用户自定义的reduce()方法）</li>
</ol>
<p>    Shuffle中的缓冲区大小会影响到mapreduce程序的执行效率，原则上说，缓冲区越大，磁盘io的次数越少，执行速度就越快</p>
<p>    缓冲区的大小可以通过参数调整,  参数：io.sort.mb  默认100M</p>
<h4 id="2-4-Partition-分区"><a href="#2-4-Partition-分区" class="headerlink" title="2.4 Partition 分区"></a>2.4 Partition 分区</h4><p><strong>2.4.1 问题引出</strong> </p>
<p>    要求将统计结果按照条件输出到不同文件中（分区）。比如：将统计结果按照手机 归属地不同省份输出到不同文件中（分区）。</p>
<p><strong>2.4.2 默认Partitioner分区</strong> </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HashPartitioner</span> <span class="keyword">extends</span> <span class="title">Partitioner</span> </span>&#123; </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getPartition</span><span class="params">(K key, V value, <span class="keyword">int</span> numReduceTasks)</span> </span>&#123; </span><br><span class="line">        <span class="keyword">return</span> (key.hashCode() &amp; Integer.MAX_VALUE) % numReduceTasks; </span><br><span class="line">    &#125; </span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<p>    默认分区是根据key的hashCode对ReduceTasks个数取模得到的。用户没法控制哪个 key存储到哪个分区。</p>
<p><strong>2.4.3 自定义Partitioner步骤</strong> </p>
<p>（1）自定义类继承Partitioner，重写getPartition()方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CustomPartitioner</span> <span class="keyword">extends</span> <span class="title">Partitioner</span> </span>&#123; </span><br><span class="line">    <span class="meta">@Override</span> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getPartition</span><span class="params">(Text key, FlowBean value, <span class="keyword">int</span> numPartitions)</span> </span>&#123; </span><br><span class="line">        <span class="comment">// 控制分区代码逻辑 … … </span></span><br><span class="line">        <span class="keyword">return</span> partition; </span><br><span class="line">    &#125; </span><br><span class="line">&#125; </span><br></pre></td></tr></table></figure>

<p>（2）在Job驱动中，设置自定义Partitioner </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">job.setPartitionerClass(CustomPartitioner.class); </span><br></pre></td></tr></table></figure>

<p>（3）自定义Partition后，要根据自定义Partitioner的逻辑设置相应数量的ReduceTask </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">job.setNumReduceTasks(<span class="number">5</span>);</span><br></pre></td></tr></table></figure>

<p><strong>2.4.4 分区总结</strong><br>(1)如果ReduceTask的数量 &gt; getPartition的结果数，则会多产生几个空的输出文件part-r-00xx ;</p>
<p>(2)如果 1 &lt; ReduceTask的数量 &lt; getPartition的结果数，则有一部分分区数据无处安放，会Exception ;</p>
<p>(3）如果ReduceTask的数量 = 1，则不管MapTask端输出多少个分区文件，最终结果都交给这一个ReduceTask，最终也就只会产生一个结果文件part-r-O00O0;</p>
<p>(4)分区号必须从零开始，逐一累加。</p>
<p>2.3.5 案例分析</p>
<p>例如∶假设自定义分区数为5，则</p>
<p>( 1 ) job.setNumReduceTasks(1); 会正常运行，只不过会产生一个输出文件</p>
<p>( 2 ) job.setNumReduceTasks(2); 会报错</p>
<p>( 3 ) job.setNumReduceTasks(6); 大于5，程序会正常运行，会产生空文件</p>
<h4 id="2-5-WritableComparable-排序"><a href="#2-5-WritableComparable-排序" class="headerlink" title="2.5 WritableComparable 排序"></a>2.5 WritableComparable 排序</h4><p><strong>2.5.1 排序概述</strong></p>
<p>    排序是MapReduce框架中最重要的操作之一。</p>
<p>    MapTask和ReduceTask均会对数据按照key进行排序。该操作属于Hadoop的默认行为。任何应用程序中的数据均会被排序，而不管逻辑上是否需要。 </p>
<p>    默认排序是按照字典顺序排序，且实现该排序的方法是快速排序。</p>
<p>    对于MapTask ，它会将处理的结果暂时放到环形缓冲区中，当环形缓冲区使用率达到一定阈值后，再对缓冲区中的数据进行一次快速排序，并将这些有序数据溢写到磁盘上，而当数据处理完毕后，它会对磁盘上所有文件进行归并排序。</p>
<p>    对于ReduceTask，它从每个MapTask上远程拷贝相应的数据文件，如果文件大小超过一定阈值，则溢写磁盘上，否则存储在内存中。如果磁盘上文件数目达到一定阈值，则进行一次归并排序以生成一个更大文件；如果内存中文件大小或者数目超过一定阈值，则进行一次合并后将数据溢写到磁盘上。当所有数据拷贝完毕后，ReduceTask统一对内存和磁盘上的所有数据进行一次归并排序。</p>
<p><strong>2.5.2 排序分类</strong></p>
<p>(1)部分排序</p>
<p>    MapReduce根据输入记录的键对数据集排序。保证输出的每个文件内部有序。</p>
<p>(2）全排序</p>
<p>    最终输出结果只有一个文件，且文件内部有序。实现方式是只设置一个ReduceTask。但该方法在处理大型文件时效率极低，因为一台机器处理所有文件，完全丧失了MapReduce所提供的并行架构。</p>
<p>(3）辅助排序∶( GroupingComparator分组)</p>
<p>    在Reduce端对key进行分组。应用于∶在接收的key为bean对象时，想让一个或几个字段相同（全部字段比较不相同）的key进入到同一个reduce方法时，可以采用分组排序。</p>
<p>(4)二次排序</p>
<p>    在自定义排序过程中，如果compareTo中的判断条件为两个即为二次排序。</p>
<h4 id="2-6-Combiner-合并"><a href="#2-6-Combiner-合并" class="headerlink" title="2.6 Combiner 合并"></a>2.6 Combiner 合并</h4><p>( 1 )Combiner是MR程序中Mapper和Reducer之外的一种组件。</p>
<p>( 2 ) Combiner组件的父类就是Reducer。</p>
<p>( 3 )Combiner和Reducer的区别在于运行的位置</p>
<ul>
<li>  Combiner是在每一个MapTask所在的节点运行;</li>
<li>  Reducer是接收全局所有Mapper的输出结果;</li>
</ul>
<p>( 4 ) Combiner的意义就是对每一个MapTask的输出进行局部汇总，以减小网络传输量。</p>
<p>( 5 ) Combiner能够应用的前提是不能影响最终的业务逻辑，而且，Combiner的输出kv应该跟Reducer的输入kv类型要对应起来。</p>
<p><img src="/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9Ashuffle%E6%9C%BA%E5%88%B6/5.png" alt="image-20210601003344002"></p>
<p>(6）自定义Combiner 实现步骤</p>
<p>（a）自定义一个Combiner 继承Reducer，重写Reduce方法</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">WordCountCombiner</span> <span class="keyword">extends</span> <span class="title">Reducer</span>&lt;<span class="title">Text</span>, <span class="title">IntWritable</span>, <span class="title">Text</span>, <span class="title">IntWritable</span>&gt; </span>&#123;</span><br><span class="line">    <span class="keyword">private</span> IntWritable outV = <span class="keyword">new</span> IntWritable();</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">protected</span> <span class="keyword">void</span> <span class="title">reduce</span><span class="params">(Text key, Iterable&lt;IntWritable&gt; values, Context context)</span> <span class="keyword">throws</span> IOException, InterruptedException </span>&#123;</span><br><span class="line">    <span class="keyword">int</span> sum = <span class="number">0</span>;</span><br><span class="line">    <span class="keyword">for</span> (IntWritable value : values) &#123;</span><br><span class="line">    sum += value.get();</span><br><span class="line">    &#125; </span><br><span class="line">    outV.set(sum);</span><br><span class="line">    context.write(key,outV);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>（b）在 Job 驱动类中设置： </p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">job.setCombinerClass(WordCountCombiner.class);</span><br></pre></td></tr></table></figure>


    </div>

    
    
    

      <footer class="post-footer">
          <div class="post-tags">
              <a href="/myblog/tags/%E6%95%B0%E6%8D%AE%E7%A0%94%E5%8F%91/" rel="tag"># 数据研发</a>
              <a href="/myblog/tags/Hadoop/" rel="tag"># Hadoop</a>
              <a href="/myblog/tags/MapReduce/" rel="tag"># MapReduce</a>
              <a href="/myblog/tags/shuffle/" rel="tag"># shuffle</a>
          </div>

        


        
    <div class="post-nav">
      <div class="post-nav-item">
    <a href="/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9AMapTask%E5%B9%B6%E8%A1%8C%E5%BA%A6%E5%86%B3%E5%AE%9A%E6%9C%BA%E5%88%B6/" rel="prev" title="MapReduce并行度决定机制">
      <i class="fa fa-chevron-left"></i> MapReduce并行度决定机制
    </a></div>
      <div class="post-nav-item">
    <a href="/myblog/2021/06/06/Hadoop%E4%B9%8BMapReduce%EF%BC%9A%E7%8E%AF%E5%A2%83%E9%85%8D%E7%BD%AE%E4%B8%8E%E6%A1%88%E4%BE%8B%E5%AE%9E%E6%93%8D/" rel="next" title="MapReduce环境配置与案例实操">
      MapReduce环境配置与案例实操 <i class="fa fa-chevron-right"></i>
    </a></div>
    </div>
      </footer>
    
  </article>
  
  
  



          </div>
          

<script>
  window.addEventListener('tabs:register', () => {
    let { activeClass } = CONFIG.comments;
    if (CONFIG.comments.storage) {
      activeClass = localStorage.getItem('comments_active') || activeClass;
    }
    if (activeClass) {
      let activeTab = document.querySelector(`a[href="#comment-${activeClass}"]`);
      if (activeTab) {
        activeTab.click();
      }
    }
  });
  if (CONFIG.comments.storage) {
    window.addEventListener('tabs:click', event => {
      if (!event.target.matches('.tabs-comment .tab-content .tab-pane')) return;
      let commentClass = event.target.classList[1];
      localStorage.setItem('comments_active', commentClass);
    });
  }
</script>

        </div>
          
  
  <div class="toggle sidebar-toggle">
    <span class="toggle-line toggle-line-first"></span>
    <span class="toggle-line toggle-line-middle"></span>
    <span class="toggle-line toggle-line-last"></span>
  </div>

  <aside class="sidebar">
    <div class="sidebar-inner">

      <ul class="sidebar-nav motion-element">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <!--noindex-->
      <div class="post-toc-wrap sidebar-panel">
          <div class="post-toc motion-element"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%B8%80%E3%80%81MapReduce%E8%AF%A6%E7%BB%86%E5%B7%A5%E4%BD%9C%E6%B5%81%E7%A8%8B"><span class="nav-text">一、MapReduce详细工作流程</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E4%BA%8C%E3%80%81shuffle%E6%9C%BA%E5%88%B6"><span class="nav-text">二、shuffle机制</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#2-1-%E6%A6%82%E8%BF%B0"><span class="nav-text">2.1 概述</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-2-%E4%B8%BB%E8%A6%81%E6%B5%81%E7%A8%8B"><span class="nav-text">2.2 主要流程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-3-%E8%AF%A6%E7%BB%86%E6%B5%81%E7%A8%8B"><span class="nav-text">2.3 详细流程</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-4-Partition-%E5%88%86%E5%8C%BA"><span class="nav-text">2.4 Partition 分区</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-5-WritableComparable-%E6%8E%92%E5%BA%8F"><span class="nav-text">2.5 WritableComparable 排序</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#2-6-Combiner-%E5%90%88%E5%B9%B6"><span class="nav-text">2.6 Combiner 合并</span></a></li></ol></li></ol></div>
      </div>
      <!--/noindex-->

      <div class="site-overview-wrap sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="高乾威"
      src="/myblog/images/avatar.png">
  <p class="site-author-name" itemprop="name">高乾威</p>
  <div class="site-description" itemprop="description">报我楼成秋望月，把君诗读夜回灯</div>
</div>
<div class="site-state-wrap motion-element">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
          <a href="/myblog/archives/">
        
          <span class="site-state-item-count">91</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
            <a href="/myblog/categories/">
          
        <span class="site-state-item-count">27</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
            <a href="/myblog/tags/">
          
        <span class="site-state-item-count">59</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="links-of-author motion-element">
      <span class="links-of-author-item">
        <a href="http://github.com/GaoQianwei" title="GitHub → http:&#x2F;&#x2F;github.com&#x2F;GaoQianwei" rel="noopener" target="_blank">GitHub</a>
      </span>
      <span class="links-of-author-item">
        <a href="mailto:2966807184@qq.com" title="E-Mail → mailto:2966807184@qq.com" rel="noopener" target="_blank">E-Mail</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://www.zhihu.com/people/xue-wu-zhi-jing-75-22" title="知乎 → https:&#x2F;&#x2F;www.zhihu.com&#x2F;people&#x2F;xue-wu-zhi-jing-75-22" rel="noopener" target="_blank">知乎</a>
      </span>
      <span class="links-of-author-item">
        <a href="https://blog.csdn.net/weixin_45341339" title="CSDN → https:&#x2F;&#x2F;blog.csdn.net&#x2F;weixin_45341339" rel="noopener" target="_blank">CSDN</a>
      </span>
  </div>



      </div><div>
  <canvas id="canvasDiyBlock" style="width:60%;">当前浏览器不支持canvas，请更换浏览器后再试</canvas>
<script src="/myblog/js/custom/clock.js"></script>

</div>


    </div>
  </aside>
  <div id="sidebar-dimmer"></div>


      </div>
    </main>

    <footer class="footer">
      <div class="footer-inner">
        

        

<div class="copyright">
  
  &copy; 2021-04 到 
  <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">高乾威</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" class="theme-link" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.org/" class="theme-link" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

<span class="post-meta-divider">|</span>

<div class="theme-info">
  <div class="powered-by"></div>
  <span class="post-count">全站共 117.2k 字</span>
</div>

<span class="post-meta-divider">|</span>
        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
</div>








      </div>
    </footer>
  </div>

  
  <script src="/myblog/lib/anime.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.min.js"></script>
  <script src="/myblog/lib/velocity/velocity.ui.min.js"></script>

<script src="/myblog/js/utils.js"></script>

<script src="/myblog/js/motion.js"></script>


<script src="/myblog/js/schemes/pisces.js"></script>


<script src="/myblog/js/next-boot.js"></script>


  <script defer src="/myblog/lib/three/three.min.js"></script>
    <script defer src="/myblog/lib/three/canvas_lines.min.js"></script>


  




  
<script src="/myblog/js/local-search.js"></script>













  

  

</body>
</html>
